<template>
  <div>
    <brand :brand1="brand1" :brand2="brand2"></brand>
    <el-card>
      <el-row :gutter="18">
        <el-col :span="12">
          <!-- 搜索区域 -->
          <el-input placeholder="请输入内容" v-model="getGoods.query" clearable @clear="getGoodsList()">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="8">
          <!-- 添加用户按钮 -->
          <el-button type="primary" @click="addGoods()">添加商品</el-button>
        </el-col>
      </el-row>
      <el-row :gutter="18">
        <el-table :data="tableData" style="width: 100%" border>
          <el-table-column type="index" width="50"></el-table-column>
          <el-table-column prop="goods_name" label="商品名称" width="700"></el-table-column>
          <el-table-column prop="goods_price" label="商品价格(元)" width="100"></el-table-column>
          <el-table-column prop="goods_weight" label="商品重量" width="100"></el-table-column>
          <el-table-column  label="创建时间" width="180">
            <template  slot-scope="scope">
              <div>
                <!-- 获取一行数据 -->
                {{ scope.row.add_time | dateFrom}}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120">
            <template  slot-scope="scope">
              <div>
                <el-button size="mini" type="primary" icon="el-icon-edit"  @click="editGoods(scope.row.goods_id)"></el-button>
                <el-button size="mini" type="danger" icon="el-icon-delete"  @click="deleteGoods(scope.row.goods_id)"></el-button>
              </div>
            </template>

          </el-table-column>
        </el-table>
      </el-row>
      <!-- 分页器 -->
      <el-pagination
        @size-change="pSizeChange"
        @current-change="pCurrentChange"
        :current-page="getGoods.pagenum"
        :page-sizes="[100, 200, 300, 400]"
        :page-size="getGoods.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      brand1: '商品管理',
      brand2: '商品列表',
      getGoods: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 总条数
      total: 0,
      tableData: []
    }
  },
  methods: {
    // 获取goods列表
    async getGoodsList() {
      let { data: res } = await this.$Http.getGoods(this.getGoods)
      // console.log(res)
      if (res.meta.status != 200) return this.$message.error(res.meta.msg)
      this.tableData = res.data.goods
      this.total = res.data.total
    },
    // 确定添加goods
    addGoods() {
      this.$router.push('goods/add')
    },
    // pagesize改变
    pSizeChange(val) {
      this.getGoods.pagesize = val
      this.getGoodsList()
    },
    // 当前页改变
    pCurrentChange(val) {
      this.getGoods.pagenum = val
      this.getGoodsList()
    },

    editGoods() {},
    deleteGoods() {
    }
  },

  created() {
    this.getGoodsList()
  }
}
</script>

<style lang='less' scoped>
.el-table {
  font-size: 12px;
  margin-left:9px;
}

</style>
